package com.eternal.device;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eternal.base.data.RepositoryInjection;
import com.eternal.base.global.ActivityEvent;
import com.eternal.base.global.BluetoothKey;
import com.eternal.base.router.RouterActivityPath;
import com.eternal.device.databinding.ActivityDeviceBinding;
import com.eternal.device.model.AddModel;
import com.eternal.framework.binding.command.BindingConsumer;
import com.eternal.framework.bus.Messenger;
import com.eternal.framework.component.BaseActivity;

/** 蓝牙扫描添加页
 * @author Administrator
 */
@Route(path = RouterActivityPath.Device.PAGE_ADD)
public class DeviceActivity extends BaseActivity<ActivityDeviceBinding, AddModel> {
    public static final String SHOW_DIALOG = "show dialog";
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_device;
    }

    @Override
    public int initVariableId() {
        return BR.model;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        byte deviceType = intent.getByteExtra(ActivityEvent.DEVICE_TYPE, BluetoothKey.DEVICE_TYPE_A);
        viewModel.init(RepositoryInjection.providerDeviceRepository(),deviceType);
        initMessage();
    }

    private void initMessage(){
        Messenger.getDefault().register(this, SHOW_DIALOG, AddModel.class, new BindingConsumer<AddModel>() {
            @Override
            public void call(AddModel itemModel) {
                View view = LayoutInflater.from(DeviceActivity.this).inflate(com.eternal.res.R.layout.dialog_tip, null, false);;
                final MaterialDialog dialog = new MaterialDialog.Builder(DeviceActivity.this)
                        .backgroundColor(0x00ffffff)
                        .customView(view, false)
                        .build();
                view.findViewById(com.eternal.res.R.id.tv_cancel)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                view=dialog.getCustomView();
                view.findViewById(com.eternal.res.R.id.tv_confirm)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                viewModel.pushToMain();
                                dialog.dismiss();
                            }
                        });
                dialog.show();
            }
        });
    }
    @Override
    public void onBackPressed() {
        viewModel.onBack.execute();
    }
    private void unregisterMassage(){
        Messenger.getDefault().unregister(this);
    }
    @Override
    protected void onDestroy() {
        unregisterMassage();
        super.onDestroy();
    }
}
